package com.chushouya.aop;

import com.chushouya.common.support.DevOption;
import com.chushouya.common.support.RequestHeaders;
import com.chushouya.common.support.RequestUserContext;
import com.chushouya.manager.dto.admin.login.AdminUserAuthDTO;
import com.chushouya.manager.service.admin.AdminTokenService;
import com.general.framework.core.enums.ContextUserType;
import com.general.framework.core.exception.CodeEnum;
import com.general.framework.core.exception.Ex;
import com.general.framework.core.lang.Longs;
import com.general.framework.core.lang.Strings;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Objects;

/**
 * Admin认证拦截器
 * @author leejean team
 */
@Slf4j
@Aspect
@Component
@Order(200)
public class AdminAuthAspect extends BaseAuthAspect<AdminUserAuthDTO>{

    @Resource
    private HttpServletRequest request;

    @Resource
    private AdminTokenService adminTokenService;

    /**
     * 用户认证处理
     */
    @Around("execution(* com.chushouya.controller.*.admin.*.*(..))")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
//        super.setHeaderContext();
        // 免登陆
        if (super.skipAuthValidate(pjp)) {
            return pjp.proceed();
        }
        final String token = request.getHeader(RequestHeaders.TOKEN);
        if (DevOption.isDevModel() && Strings.isBlank(token)) {
            super.setDevUserContext();
            return pjp.proceed();
        }
        if (Strings.isBlank(token)) {
            throw Ex.business(CodeEnum.UNAUTHORIZED);
        }
        try {
            final AdminUserAuthDTO adminUserAuth = adminTokenService.authToken(token);
            super.setUserContext(adminUserAuth);
            return pjp.proceed();
        } finally {
            super.clearUserContext();
//            super.clearHeaderContext();
        }
    }

    @Override
    protected RequestUserContext buildUserContext(AdminUserAuthDTO userAuth) {
        RequestUserContext userContext = new RequestUserContext();
        userContext.setUserId(userAuth.getAdminUserId());
        userContext.setUserName(userAuth.getUserName());
        userContext.setUserType(ContextUserType.ADMIN_PLATFORM.value());
        if (Objects.nonNull(userAuth.getExtendInfo())) {
            final Long partnerId = userAuth.getExtendInfo().getPartnerId();
            if (Longs.notNullAndZero(partnerId)){
                userContext.setPartnerId(partnerId);
                userContext.setUserType(ContextUserType.ADMIN_PARTNER.value());
            }
        }
        userContext.setRoles(userAuth.getRoles());
        userContext.setPermissions(userAuth.getPermissions());
        userContext.setExtend(new HashMap<>(1));
        return userContext;
    }
}
